<?php

namespace backend\modules\orders\models;

use backend\modules\deliveryorder\models\DeliveryGoods;
use backend\modules\deliveryorder\models\DeliveryOrder;
use backend\modules\shipping\models\EcsRegion;
use backend\modules\shipping\models\Shipping;
use Yii;
use yii\base\Exception;

/**
 * This is the model class for table "order_info".
 *
配货中
待发货
待收货 已发货
（待评价 已收货）：5order  shipping2  pay2

顺丰发货逻辑修改

待付款   pay_status 0、shipping_status 0、order_status 1、cancel_status 0
、、付款中 pay_status 1
待发货	pay_staus 2、shipping_status 0、order_status 1
已发货、待收货	pay_staus 2、shipping_status 1、order_status 2
、、配货中 pay_status 2、shipping_status 3、order_status 1
、、发货中 pay_status 2、shipping_status 5、order_status 5
交易评价完成、已收货、待评价 pay_status 2、shipping_status 2、comment_status 0、order_status 3
评价完成、 pay_status 2、shipping_status 2、comment_status 1、order_status 4
取消订单 pay_status 0、shipping_status 0、order_status 1、cancel_status 1、order_status 5
删除订单 pay_status 0、shipping_status 0、order_status 1、cancel_status 1、order_status 6

0 1 2 3 4 5 6 7 8
 * @property string $order_id
 * @property string $order_sn
 * @property string $user_id
 * @property integer $order_status
 * @property integer $shipping_status
 * @property integer $pay_status
 * @property string $consignee
 * @property integer $country
 * @property integer $province
 * @property integer $city
 * @property integer $district
 * @property string $address
 * @property string $tel
 * @property string $mobile
 * @property string $email
 * @property string $best_time
 * @property integer $shipping_id
 * @property string $shipping_name
 * @property integer $pay_id
 * @property string $pay_name
 * @property string $goods_amount
 * @property string $shipping_fee
 * @property string $pay_fee
 * @property string $money_paid
 * @property string $order_amount
 * @property string $referer
 * @property string $add_time
 * @property string $confirm_time
 * @property string $pay_time
 * @property string $shipping_time
 * @property string $invoice_no
 * @property string $to_buyer
 * @property string $pay_note
 * @property string $parent_id
 * @property string $discount
 * @property string $transaction_id
 * @property string $shopping_temp
 * @property integer $updated_at
 * @property integer $comment_status
 * @property integer $is_delete
 * @property string $order_check_money
 * @property string $order_user_money
 * @property string $order_should_check_money
 * @property integer $is_without_pay
 * @property integer $cancel_status
 * @property string $user_name
 * @property integer $created_at
 * @property integer $is_warm
 */
class OrderInfo extends \yii\db\ActiveRecord
{
    public $country_name;
    public $province_name;
    public $city_name;
    public $district_name;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order_info';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
//            TimestampBehavior::className(),
            // 名为myBehavior4的行为，也是给出了MyBehavior类的配置数组
            'uuid' => [
                'class' => \common\behavior\uuidBehavior::className(),
                'owner' => $this
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['order_id'], 'required'],
            [['order_status','is_warm', 'is_share', 'shipping_status', 'pay_status', 'country', 'province', 'city', 'district', 'shipping_id', 'pay_id', 'add_time', 'confirm_time', 'shipping_time', 'parent_id', 'updated_at', 'comment_status', 'is_delete', 'is_without_pay', 'cancel_status', 'created_at'], 'integer'],
            [['goods_amount', 'shipping_fee', 'pay_fee',  'money_paid', 'order_amount', 'discount', 'order_check_money', 'order_user_money', 'order_should_check_money'], 'number'],
            [['shopping_temp'], 'string'],
            [['order_id', 'user_id'], 'string', 'max' => 40],
            [['order_sn'], 'string', 'max' => 20],
            [['consignee', 'tel', 'mobile', 'email', 'user_name'], 'string', 'max' => 60],
            [['address', 'referer', 'invoice_no', 'to_buyer'], 'string', 'max' => 255],
            [['best_time', 'shipping_name', 'pay_name'], 'string', 'max' => 120],
            [['pay_time'], 'string', 'max' => 50],
            [['transaction_id'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'order_id' => Yii::t('app', 'Order ID'),
            'order_sn' => Yii::t('app', '订单号'),
            'user_id' => Yii::t('app', 'User ID'),
            'user_name'=>Yii::t('app', '用户'),
            'order_status' => Yii::t('app', '订单状态(0未支付、 1已支付待发货、2已支付已发货待收货、3已支付已发货已收货待评价、4支付已发货已收货已评价、5未支付已取消、6已删除)'),
            'shipping_status' => Yii::t('app', '发货状态'),
            'pay_status' => Yii::t('app', '是否支付'),
            'comment_status' => Yii::t('app', '是否评论'),
            'cancel_status' => Yii::t('app', '是否已取消'),
            'is_delete' => Yii::t('app', '是否删除'),
            'consignee' => Yii::t('app', '收货人'),
            'country' => Yii::t('app', '国家'),
            'province' => Yii::t('app', '省份'),
            'city' => Yii::t('app', '城市'),
            'district' => Yii::t('app', '区域'),
            'address' => Yii::t('app', '详细地址'),
            'tel' => Yii::t('app', '电话号码'),
            'mobile' => Yii::t('app', '手机号码'),
            'email' => Yii::t('app', 'Email'),
            'best_time' => Yii::t('app', '送货最佳时间'),
            'shipping_id' => Yii::t('app', 'Shipping ID'),
            'shipping_name' => Yii::t('app', '物流方式'),
            'pay_id' => Yii::t('app', 'Pay ID'),
            'pay_name' => Yii::t('app', '支付方式'),
            'goods_amount' => Yii::t('app', '用户订单实际需支付总价'),
            'shipping_fee' => Yii::t('app', '邮费'),
            'pay_fee' => Yii::t('app', 'Pay Fee'),
            'money_paid' => Yii::t('app', '实际已支付总价'),
            'order_amount' => Yii::t('app', '订单正常总价'),
            'referer' => Yii::t('app', 'Referer'),
            'add_time' => Yii::t('app', '下单时间'),
            'confirm_time' => Yii::t('app', 'Confirm Time'),
            'pay_time' => Yii::t('app', 'Pay Time'),
            'shipping_time' => Yii::t('app', 'Shipping Time'),
            'invoice_no' => Yii::t('app', '物流号'),
            'to_buyer' => Yii::t('app', 'To Buyer'),
            'parent_id' => Yii::t('app', 'Parent ID'),
            'discount' => Yii::t('app', '折扣'),
            'transaction_id' => Yii::t('app', '支付编号'),
            'shopping_temp' => Yii::t('app', '电子面单'),
            'updated_at' => Yii::t('app', '更新时间'),
            'created_at' => Yii::t('app', '下单时间'),
            'order_check_money' => Yii::t('app', 'Order Check Money'),
            'order_user_money' => Yii::t('app', 'Order User Money'),
            'order_should_check_money' => Yii::t('app', 'Order Should Check Money'),
            'is_without_pay' => Yii::t('app', '是否不用支付'),
        ];
    }

    /**
     * @inheritdoc
     * @return \backend\modules\orders\models\query\OrderInfoQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \backend\modules\orders\models\query\OrderInfoQuery(get_called_class());
    }

    /**
     * (0未支付、 1已支付待发货、2已支付已发货待收货、3已支付已发货已收货待评价、4支付已发货已收货已评价、5未支付已取消、6已删除)
     * @param $type
     * @return array|string
     */
    public static function orderStatusAlias($type)
    {
        if(empty($type)){
            return "未支付";
        } else if($type==1){
            return "已支付、待发货";
        }else if($type==2){
            return "已支付、已发货、待收货";
        }else if($type==3){
            return "待评价";
        }else if($type==4){
            return "已评价";
        }else if($type==5){
            return "已取消";
        }else if($type==6){
            return "已删除";
        }else if($type=='all'){
            return ["未支付","已支付、待发货","已支付、已发货、待收货","待评价","已评价","已取消","已删除",];
        }
    }

    /**
     * 获取物流列表，订单物流选中
     *
     * @param $id
     * @return array|\backend\modules\shipping\models\Shipping[]
     */
    public function getShippingByOrderId($id)
    {
        $order = OrderInfo::findOne($id);
        $shipping = Shipping::find()->asArray()->all();
        $shippingARR = array();
        if($order->shipping_id){
            foreach($shipping as $k=>$v) {
                if($v['shipping_id']==$order->shipping_id){
                    $v['check'] = 1;
                    $v['invoice_no'] = $order->invoice_no;
                    $v['shipping_temp'] = $order->shopping_temp;
                } else {
                    $v['check'] = 0;
                    $v['invoice_no'] = '';
                    $v['shipping_temp'] = '';
                }
                $shippingARR[$k] = $v;
            }
        } else {
            return $shipping;
        }
        return $shippingARR;
    }


    /**
     * 生成发货单
     *
     * @param $OrderInfo
     * @return bool
     * @throws Exception
     */
    public function getDeliveryGoodsM($OrderInfo)
    {
        $deliveryOrder = new DeliveryOrder();
        foreach($OrderInfo  as $k=>$v)  {
            $getAttributes = $deliveryOrder->getAttributes();
            if($k=='add_time') {
                continue;
            }
            if(array_key_exists($k, $getAttributes)){
                $deliveryOrder->{$k} = $v;
            }
        }

        if(!$deliveryOrder->save()) {
            throw new Exception(json_encode($deliveryOrder->errors));
        }

        $orderGoodsARR = OrderGoods::find()->where(['order_id'=>$OrderInfo['order_id']])->asArray()->all();
        foreach($orderGoodsARR as $k=>$v) {
            $deliveryGoods = new DeliveryGoods();
            $deliveryGoods->delivery_order_id = $deliveryOrder->delivery_order_id;
            $deliveryGoods->goods_name = $v['goods_name'];
            $deliveryGoods->send_number = $v['send_number'];
            $deliveryGoods->goods_id = $v['goods_id'];
            if(!$deliveryGoods->save()) {
                throw new Exception(json_encode($deliveryGoods->errors));
            }
        }
        return true;
    }

    public function afterFind()
    {
//        $addec = new \backend\modules\orders\lib\AddExchange();
//        $addec->exchangeObj($this);
    }
}
